{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b0940c21",
   "metadata": {},
   "source": [
    "## 易错点\n",
    "1. from mlxtend.preprocessing import TransactionEncoder ,TransactionEncoder导包路径\n",
    "2. te_data = te.fit_transform(transactions) 括号里面是transactions而非df\n",
    "3. antecedent_sale = rules['antecedents'] == frozenset({'Banana'}) 忘记了 \n",
    "frozenset作用：\n",
    "Build an immutable unordered collection of unique elements.\n",
    "构建一个不可变的无序唯一元素集合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "db2f7462",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from mlxtend.frequent_patterns import apriori\n",
    "import warnings\n",
    "warnings.filterwarnings(\"ignore\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c0959155",
   "metadata": {},
   "source": [
    "1.对df中的items列应用lambda函数，将每行中的值按照逗号分隔的字符串拆分成列表，并去掉空格，将结果存储在transaction列表中。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8c2d8379",
   "metadata": {},
   "outputs": [],
   "source": [
    "#读取数据\n",
    "df = pd.read_csv('./data/transactions.csv')\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "46cd6294",
   "metadata": {},
   "outputs": [],
   "source": [
    "#由考生填写\n",
    "transactions = df['items'].map(lambda line:line.replace(' ','').split(','))\n",
    "transactions\n",
    "#由考生填写"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "862ca349",
   "metadata": {},
   "source": [
    "2.使用TransactionEncoder将数transaction转化为适合Apriori法的格式数据te_data\n",
    "使用Pandas的DataFrame()函数将te_date转换为DataFrame对象df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6bd716a2",
   "metadata": {},
   "outputs": [],
   "source": [
    "#由考生填写\n",
    "from mlxtend.preprocessing import TransactionEncoder\n",
    "te = TransactionEncoder()\n",
    "data_te = te.fit_transform(transactions)\n",
    "df_te = pd.DataFrame(data=data_te,columns=te.columns_)\n",
    "df_te\n",
    "#由考生填写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bdfb32d2",
   "metadata": {},
   "outputs": [],
   "source": [
    "from mlxtend.frequent_patterns import fpgrowth\n",
    "from mlxtend.frequent_patterns import association_rules\n",
    "# fpgrowth\n",
    "frequent_itemsets = fpgrowth(df_te,min_support=0.5,use_colnames=True)\n",
    "# model = fpgrowth(df_te,min_support=0.3,use_colnames=True)\n",
    "# 输出频繁项集\n",
    "frequent_itemsets"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8f996074",
   "metadata": {},
   "source": [
    " 3,从频繁项集frequent_itemsets中构建关联规则rules,度量指标位confidence,最小阈值为0.5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6bc23e2c",
   "metadata": {},
   "outputs": [],
   "source": [
    "#由考生填写\n",
    "rules = association_rules(df=frequent_itemsets,metric='confidence',min_threshold=0.5,support_only=True,)\n",
    "rules\n",
    "#由考生填写\n",
    "# 打印关联规则\n",
    "print(rules)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5300396d",
   "metadata": {},
   "source": [
    "4.打印出同时满足先验支持度>=0.6,支持度>0.3,提升度>0.8的rules数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4ad572e4",
   "metadata": {},
   "outputs": [],
   "source": [
    "#由考生填写\n",
    "rules[(rules['antecedent support']>=0.6) & (rules['support']>0.3) & (rules['lift']>0.8)]\n",
    "#由考生填写"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8713ff3f",
   "metadata": {},
   "source": [
    "5.筛选规则rules,将规则中同时满足antecedent_sale为Banana,consequent_sale为Apple的规则选出来，\n",
    "并存储在final_sale中,使得rules.loc[final_sale]输出"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e6bd54cc",
   "metadata": {},
   "outputs": [],
   "source": [
    "#由考生填写\n",
    "antecedent_sale = rules['antecedents'] == frozenset({'banana'})\n",
    "consequent_sale = rules['consequents'] == frozenset({'apple'})\n",
    "final_sale = (consequent_sale & antecedent_sale)\n",
    "#由考生填写\n",
    "rules.loc[final_sale]"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "PySpark-2.4.5",
   "language": "python",
   "name": "pyspark-2.4.5"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
